home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / fadeflip / oglwindow.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  245 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include "oglwindow.h"
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. static int RGBA_SB_attributes[] = {
  45.   GLX_RGBA,
  46.   GLX_RED_SIZE, 1,
  47.   GLX_GREEN_SIZE, 1,
  48.   GLX_BLUE_SIZE, 1,
  49.   GLX_DEPTH_SIZE, 1,
  50.   None,
  51. };
  52.  
  53. static int RGBA_DB_attributes[] = {
  54.   GLX_RGBA,
  55.   GLX_RED_SIZE, 1,
  56.   GLX_GREEN_SIZE, 1,
  57.   GLX_BLUE_SIZE, 1,
  58.   GLX_DOUBLEBUFFER,
  59.   GLX_DEPTH_SIZE, 1,
  60.   None,
  61. };
  62.  
  63. oglwindow::oglwindow()
  64. {
  65.   doublebuffer = 1;
  66.   minx = miny = maxx = maxy = 0;
  67.   event_mask = ExposureMask | StructureNotifyMask | KeyPressMask |
  68.     ButtonPressMask;
  69.   strncpy(title, "OpenGL", max_titlelength);
  70. }
  71.  
  72. void oglwindow::set_doublebuffer(int new_doublebuffer)
  73. {
  74.   doublebuffer = new_doublebuffer;
  75. }
  76.  
  77. void oglwindow::set_minsize(int new_minx, int new_miny) 
  78. {
  79.   minx = new_minx;
  80.   miny = new_miny;
  81. }
  82.  
  83. void oglwindow::set_maxsize(int new_maxx, int new_maxy)
  84. {
  85.   maxx = new_maxx;
  86.   maxy = new_maxy;
  87. }
  88.  
  89. void oglwindow::set_title(char *new_title)
  90. {
  91.   strncpy(title, new_title, max_titlelength);
  92. }
  93.  
  94. void oglwindow::set_event_mask(unsigned long new_event_mask) 
  95. {
  96.   event_mask = new_event_mask;
  97. }
  98.  
  99. void oglwindow::add_event_mask(unsigned long new_flag)
  100. {
  101.   event_mask |= new_flag;
  102. }
  103.  
  104. unsigned long oglwindow::get_event_mask()
  105. {
  106.   return event_mask;
  107. }
  108.  
  109. int oglwindow::get_width() 
  110. {
  111.   XWindowAttributes winattr;
  112.   XGetWindowAttributes(dpy, window, &winattr);
  113.   return winattr.width;
  114. }
  115.  
  116. int oglwindow::get_height() 
  117. {
  118.   XWindowAttributes winattr;
  119.   XGetWindowAttributes(dpy, window, &winattr);
  120.   return winattr.height;
  121. }
  122.  
  123. char *oglwindow::get_title()
  124. {
  125.   return title;
  126. }
  127.  
  128. void oglwindow::open() 
  129. {
  130.   int can_ogl, can_zbuffer, can_rgba;
  131.   XSetWindowAttributes swa;
  132.   XSizeHints hints;
  133.  
  134.   dpy = XOpenDisplay(0);
  135.   if (dpy == NULL) {
  136.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  137.     exit(1);
  138.   }
  139.  
  140.   vi = glXChooseVisual(dpy, DefaultScreen(dpy), 
  141.                doublebuffer ? RGBA_DB_attributes : 
  142.                RGBA_SB_attributes);
  143.   if (vi == NULL) {
  144.     fprintf(stderr, "No matching visual on \"%s\"\n",
  145.         getenv("DISPLAY"));
  146.     exit(1);
  147.   }
  148.  
  149.   glXGetConfig(dpy, vi, GLX_USE_GL, &can_ogl);
  150.   if (!can_ogl) {
  151.     system("inform 'Your system must support OpenGL to run this program'");
  152.     exit(1);
  153.   }
  154.   glXGetConfig(dpy, vi, GLX_RGBA, &can_rgba);
  155.   if (!can_rgba) {
  156.     system("inform 'Your system must support RGB mode to run this program'");
  157.     exit(1);
  158.   }
  159.   glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &can_zbuffer);
  160.   if (can_zbuffer == 0) {
  161.     system("inform 'Your system must have a z-buffer to run this program'");
  162.     exit(1);
  163.   }
  164.  
  165.   swa.border_pixel = 0;
  166.   swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  167.                  vi->visual, AllocNone);
  168.   swa.event_mask = event_mask;
  169.   window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10, 50, 50,
  170.              0, vi->depth, InputOutput, vi->visual,
  171.              CWBorderPixel | CWColormap | CWEventMask, &swa);
  172.   XStoreName(dpy, window, title);
  173.  
  174.   hints.flags = 0;
  175.   if (minx && miny) {
  176.     hints.min_width = minx;
  177.     hints.min_height = miny;
  178.     hints.flags |= PMinSize;
  179.   }
  180.   if (maxx && maxy) {
  181.     hints.max_width = maxx;
  182.     hints.max_height = maxy;
  183.     hints.flags |= PMaxSize;
  184.   } 
  185.   if (!hints.flags) {
  186.     hints.min_aspect.x = hints.min_aspect.y =
  187.       hints.max_aspect.x = hints.max_aspect.y = 1;
  188.     hints.flags = PAspect;
  189.   }
  190.   XSetNormalHints(dpy, window, &hints);
  191.  
  192. }
  193.  
  194. static Bool WaitForMapNotify(Display */* d */, XEvent *e, char *arg)
  195. {
  196.   if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  197.     return GL_TRUE;
  198.   }
  199.   return GL_FALSE;
  200. }
  201.  
  202. void oglwindow::map() 
  203. {
  204.   XEvent ev;
  205.  
  206.   XMapWindow(dpy, window);
  207.   XIfEvent(dpy, &ev, WaitForMapNotify, (char *)window);
  208.  
  209.   ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  210. }
  211.  
  212. void oglwindow::winset()
  213. {
  214.   glXMakeCurrent(dpy, window, ctx);
  215. }
  216.  
  217. void oglwindow::swapbuffers()
  218. {
  219.   glXSwapBuffers(dpy, window);
  220. }
  221.  
  222. void oglwindow::resize() 
  223. {
  224.   XWindowAttributes windowattr;
  225.  
  226.   XGetWindowAttributes(dpy, window, &windowattr);
  227.   glViewport(0, 0, windowattr.width, windowattr.height);
  228. }
  229.  
  230. Display *oglwindow::get_display() 
  231. {
  232.   return dpy;
  233. }
  234.  
  235. Window oglwindow::get_window()
  236. {
  237.   return window;
  238. }
  239.  
  240. int oglwindow::get_doublebuffer() 
  241. {
  242.   return doublebuffer;
  243. }
  244.  
  245.